import React, { Component } from 'react'
import { SearchOutlined } from '@ant-design/icons';
import { Button, Space, Input, message } from 'antd';
import pupsub from 'pubsub-js'
import $api from '../../../../api/index';

export default class SearchInput extends Component {
  state = {
    keyword: '',
    isLoading: false
  }
  onSetName = (e) => {
    this.setState({ keyword: e.target.value })
  }
  onSearch = async () => {
    if(this.state.keyword === '') {
      message.error({ content: '用户名称不能为空'});
      return;
    }
    this.setState({ isLoading: true })
    pupsub.publish('updateLoading', true)
    const res = await $api.testApi.getGithubUser(this.state.keyword)
    this.setState({ isLoading: false })
    pupsub.publish('updateLoading', false)
    pupsub.publish('updateData', res)
  }
  onSearchData = async () => {
    this.setState({ isLoading: true })
    const res = await $api.testApi.getToken({loginName: 'admin', loginPwd: '123456'})
    console.log(res);
    this.setState({ isLoading: false })
  }
  render() {
    return (
      <Space.Compact style={{ }}>
        <Input placeholder="请输入用户名关键字" allowClear onChange={this.onSetName} onPressEnter={this.onSearch} value={this.state.keyword} />
        <Button type="primary" icon={<SearchOutlined />} onClick={this.onSearch} loading={this.state.isLoading}>搜索</Button>
        <Button type="primary" icon={<SearchOutlined />} onClick={this.onSearchData} loading={this.state.isLoading}>请求Token</Button>
      </Space.Compact>
    )
  }
}
